package com.luomo.wenyuan.modules.wenyuan.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.luomo.wenyuan.common.utils.PageUtils;
import com.luomo.wenyuan.common.utils.R;
import com.luomo.wenyuan.modules.wenyuan.dao.TOrderBookDao;
import com.luomo.wenyuan.modules.wenyuan.dao.TPutinBookDao;
import com.luomo.wenyuan.modules.wenyuan.entity.*;
import com.luomo.wenyuan.modules.wenyuan.service.*;
import com.luomo.wenyuan.modules.wenyuan.vo.TPutInSubmitVo;
import com.luomo.wenyuan.modules.wenyuan.vo.TPutinBookVo;
import com.luomo.wenyuan.modules.wenyuan.vo.TPutinSubmitBookVo;
import com.luomo.wenyuan.modules.wenyuan.vo.TPutinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @author
 * @email
 * @date 2020-10-19 15:58:39
 */
@RestController
@RequestMapping("generator/tputin")
public class TPutinController {
    @Autowired
    private TPutinService tPutinService;

    @Autowired
    private TOrderBookDao tOrderBookDao;

    @Autowired
    private TBookDetailService tBookDetailService;

    @Autowired
    private TOrderService tOrderService;

    @Autowired
    private TPutinBookService tPutinBookService;

    @Autowired
    private TPutinBookDao tPutinBookDao;

    @Autowired
    private TBookService tBookService;

    @Autowired
    private TStoreService tStoreService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tPutinService.queryPage(params);
        List<TPutinEntity> list = (List<TPutinEntity>) page.getList();
        if (CollectionUtil.isNotEmpty(list)) {
            List<TPutinVo> tPutinVos = new ArrayList<>();
            TPutinVo tPutinVo;
            for (TPutinEntity tPutinEntity : list) {
                tPutinVo = new TPutinVo();
                BeanUtil.copyProperties(tPutinEntity, tPutinVo);
                Integer orderId = tPutinEntity.getOrderId();
                Integer putinId = tPutinEntity.getId();
                tPutinVo.setStoreNameList(tPutinService.getStoreNameStrByPutinId(putinId));
                tPutinVo.setBookNameList(tOrderService.getBookNameStrByOrderId(orderId));
                List<TOrderBookEntity> orderBooks = tOrderBookDao.getByOrderId(tPutinEntity.getOrderId());
                List<TPutinBookVo> tPutinBookVos = new ArrayList<>();
                TPutinBookVo putinBookVo;
                for (TOrderBookEntity orderBook : orderBooks) {
                    putinBookVo = new TPutinBookVo();
                    Integer bookId = orderBook.getBookId();
                    TBookEntity book = tBookService.getById(bookId);
                    BeanUtil.copyProperties(book,putinBookVo);
                    List<TPutinBookEntity> putinBooks = tPutinBookDao.getByPutinIdAndBookId(putinId, bookId);
                    putinBookVo.setNum(putinBooks.size());
                    TStoreEntity tStoreEntity = tStoreService.getById(putinBooks.get(0).getStoreId());
                    putinBookVo.setStoreName(tStoreEntity.getName());
                    tPutinBookVos.add(putinBookVo);
                }
                tPutinVo.setBookList(tPutinBookVos);
                tPutinVos.add(tPutinVo);
            }
            page.setList(tPutinVos);
        }
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TPutinEntity tPutin = tPutinService.getById(id);

        return R.ok().put("tPutin", tPutin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody TPutInSubmitVo tPutInSubmitVo) {
        TPutinEntity tPutin = new TPutinEntity();
        int orderId = tPutInSubmitVo.getOrderId();
        tPutin.setOrderId(orderId);
        tPutin.setStatus(1);
        Date now = new Date();
        tPutin.setOperateDate(now);
        tPutinService.save(tPutin);
        Integer putinId = tPutin.getId();
        List<TPutinSubmitBookVo> submitBookVos = tPutInSubmitVo.getBookList();
        for (TPutinSubmitBookVo submitBookVo : submitBookVos) {
            Integer bookId = submitBookVo.getBookId();
            Integer num = submitBookVo.getNum();
            int storeId = submitBookVo.getStoreId();
            TBookDetailEntity tBookDetailEntity;
            TPutinBookEntity tPutinBookEntity;
            for (int i = 0; i < num; i++) {
                tBookDetailEntity = new TBookDetailEntity();
                tBookDetailEntity.setBookId(bookId);
                tBookDetailEntity.setStatus(0);
                tBookDetailEntity.setStoreId(storeId);
                tBookDetailEntity.setFlag(0);
                tBookDetailService.save(tBookDetailEntity);
                Integer bookDetailId = tBookDetailEntity.getId();
                tPutinBookEntity = new TPutinBookEntity();
                tPutinBookEntity.setPutinId(putinId);
                tPutinBookEntity.setBookDetailId(bookDetailId);
                tPutinBookEntity.setStoreId(storeId);
                tPutinBookService.save(tPutinBookEntity);
            }
        }
        UpdateWrapper<TOrderEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", orderId);
        updateWrapper.set("order_status", 3);
        tOrderService.update(updateWrapper);
        return R.ok();
    }

    @RequestMapping("/putin/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R putin(@PathVariable("id") Integer id) {
        Date date = new Date();
        UpdateWrapper<TPutinEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", 2);
        tPutinService.update(updateWrapper);
        List<TPutinBookEntity> putinBooks = tPutinBookDao.getByPutinId(id);
        for (TPutinBookEntity putinBook : putinBooks) {
            Integer bookDetailId = putinBook.getBookDetailId();
            TBookDetailEntity bookDetailEntity = tBookDetailService.getById(bookDetailId);
            bookDetailEntity.setStatus(1);
            bookDetailEntity.setStoreId(putinBook.getStoreId());
            bookDetailEntity.setStoreDate(date);
            putinBook.setPutinDate(date);
            tPutinBookService.updateById(putinBook);
            tBookDetailService.updateById(bookDetailEntity);
        }
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TPutinEntity tPutin) {
        tPutinService.updateById(tPutin);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        tPutinService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
